package com.kfm.shop;

import com.kfm.shop.config.LoadBalancerConfiguration;
import feign.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.reactive.ReactiveLoadBalancer;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
import org.springframework.cloud.loadbalancer.core.RandomLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.cloud.loadbalancer.support.ServiceInstanceListSuppliers;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.web.client.RestTemplate;

import javax.swing.*;

@SpringBootApplication
@EnableFeignClients // 开启 Feign 客户端
@LoadBalancerClient(configuration = LoadBalancerConfiguration.class, name = "product-service")
public class OrderApplication {

    @Bean
    // 介入 Ribbon 开启负载均衡
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    // @Bean
    // public ReactiveLoadBalancer<ServiceInstance> reactorServiceInstanceLoadBalancer(Environment environment,
    //                                                                   LoadBalancerClientFactory loadBalancerClientFactory) {
    //     String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
    //     return new RandomLoadBalancer(loadBalancerClientFactory.getProvider(name, ServiceInstanceListSupplier.class), name);
    // }

    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
        System.out.println("order 服务启动...");
    }

    @Bean
    public Logger.Level getLogLevel() {
        return Logger.Level.HEADERS; // 开启 Feign 日志 NONE BASE HEADERS FULL
    }
}
